package com.yuelao.yuelao_backend.controller;

import com.yuelao.yuelao_backend.common.ApiResponse;
import com.yuelao.yuelao_backend.dto.MiningPoolDTO;
import com.yuelao.yuelao_backend.dto.MiningStatsDTO;
import com.yuelao.yuelao_backend.dto.UserMiningInfoDTO;
import com.yuelao.yuelao_backend.dto.request.CalculateRewardsRequest;
import com.yuelao.yuelao_backend.dto.request.ClaimRequest;
import com.yuelao.yuelao_backend.dto.request.StakeRequest;
import com.yuelao.yuelao_backend.dto.request.UnstakeRequest;
import com.yuelao.yuelao_backend.service.MiningService;
import com.yuelao.yuelao_backend.util.UserContextUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import java.math.BigDecimal;
import java.util.List;

@RestController
@RequestMapping("/mining")
@Slf4j
public class MiningController {

    @Resource
    private MiningService miningService;

    /**
     * 获取挖矿池列表
     * GET /mining/pools
     * @return 挖矿池列表
     */
    @GetMapping("/pools")
    public ApiResponse<List<MiningPoolDTO>> getMiningPools() {
        List<MiningPoolDTO> pools = miningService.getMiningPools();
        return ApiResponse.ok(pools);
    }

    /**
     * 获取用户挖矿信息
     * GET /mining/user-info
     * @return 用户挖矿信息
     */
    @GetMapping("/user-info")
    public ApiResponse<UserMiningInfoDTO> getUserMiningInfo() {
        Long currentUserId = UserContextUtil.getCurrentUserId();
        UserMiningInfoDTO info = miningService.getUserMiningInfo(currentUserId);
        return ApiResponse.ok(info);
    }

    /**
     * 质押到挖矿池
     * POST /mining/stake
     * @param request 质押请求
     * @return 是否成功
     */
    @PostMapping("/stake")
    public ApiResponse<Boolean> stakeToPool(@Valid @RequestBody StakeRequest request) {
        Long currentUserId = UserContextUtil.getCurrentUserId();
        boolean result = miningService.stakeToPool(currentUserId, request.getPoolId(), request.getAmount());
        return ApiResponse.ok(result);
    }

    /**
     * 领取挖矿奖励
     * POST /mining/claim
     * @param request 领取请求
     * @return 领取奖励金额
     */
    @PostMapping("/claim")
    public ApiResponse<BigDecimal> claimRewards(@Valid @RequestBody ClaimRequest request) {
        Long currentUserId = UserContextUtil.getCurrentUserId();
        BigDecimal rewards = miningService.claimRewards(currentUserId, request.getStakingId());
        return ApiResponse.ok(rewards);
    }

    /**
     * 提取质押
     * POST /mining/unstake
     * @param request 提取请求
     * @return 提取金额
     */
    @PostMapping("/unstake")
    public ApiResponse<BigDecimal> unstakeFromPool(@Valid @RequestBody UnstakeRequest request) {
        Long currentUserId = UserContextUtil.getCurrentUserId();
        BigDecimal amount = miningService.unstakeFromPool(currentUserId, request.getStakingId(), request.getAmount());
        return ApiResponse.ok(amount);
    }

    /**
     * 获取挖矿统计数据
     * GET /mining/stats
     * @return 挖矿统计数据
     */
    @GetMapping("/stats")
    public ApiResponse<MiningStatsDTO> getMiningStats() {
        MiningStatsDTO stats = miningService.getMiningStats();
        return ApiResponse.ok(stats);
    }

    /**
     * 挖矿收益计算器
     * GET /mining/calculate
     * @param request 计算请求
     * @return 预计收益
     */
    @GetMapping("/calculate")
    public ApiResponse<BigDecimal> calculateRewards(@Valid @ModelAttribute CalculateRewardsRequest request) {
        BigDecimal estimatedRewards = miningService.calculateRewards(request.getPoolId(), request.getAmount(), request.getDuration());
        return ApiResponse.ok(estimatedRewards);
    }

    /**
     * 获取挖矿活动信息
     * GET /mining/events
     * @return 挖矿活动信息列表
     */
    @GetMapping("/events")
    public ApiResponse<List<String>> getMiningEvents() {
        List<String> events = miningService.getMiningEvents();
        return ApiResponse.ok(events);
    }
}